package com.malljelly.study.config;


import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;

/**
 * es的配置
 */
@Configuration
@Slf4j
public class ElasticsearchConfiguration {


    @Bean
    public TransportClient transportClient() {
        log.info("es初始化开始。。。。。");
        TransportClient client = null;
        try {
            TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("localhost"),
                    9300);

            // 配置信息
            Settings esSetting = Settings.builder()
                    .put("cluster.name","my-application")
                    .build();

            //配置信息Settings自定义
            client = new PreBuiltTransportClient(esSetting);
            client.addTransportAddresses(transportAddress);

        } catch (Exception e) {
            log.error("elasticsearch TransportClient create error!!!", e);
        }

        return client;
    }



}
